package org.derusse.cycle.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.provider.BaseColumns;

public class PointTable implements BaseColumns {

	public static final class Structure implements BaseColumns {
		// This class cannot be instantiated
		private Structure() {
		}

		public static final String TABLE_NAME = "point";

		public static final String TRACK = "track";
		public static final String DATE = "date";
		public static final String LATITUDE = "latitude";
		public static final String LONGITUDE = "longitude";
		public static final String ALTITUDE = "altitude";
		public static final String SPEED = "speed";
	}

	public static void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + Structure.TABLE_NAME + " ("
				+ Structure.TRACK + " LONG," + Structure.DATE + " LONG,"
				+ Structure.LATITUDE + " FLOAT," + Structure.LONGITUDE
				+ " FLOAT," + Structure.ALTITUDE + " INTEGER,"
				+ Structure.SPEED + " FLOAT);");
	}

	public static void onUpgrade(SQLiteDatabase db, int oldVersion,
			int newVersion) {
		if (oldVersion < 5) {
			db.execSQL("DROP TABLE IF EXISTS " + Structure.TABLE_NAME);
			onCreate(db);
		}
	}

	public static long Insert(SQLiteDatabase db, long track, Location location) {
		ContentValues values = new ContentValues();
		values.put(Structure.TRACK, track);
		values.put(Structure.DATE, location.getTime());
		values.put(Structure.LATITUDE, location.getLatitude());
		values.put(Structure.LONGITUDE, location.getLongitude());
		values.put(Structure.ALTITUDE, (location.hasAltitude() ? ""
				+ location.getAltitude() : "NULL"));
		values.put(Structure.SPEED, "NULL");// (location.hasSpeed()?""+location.getSpeed():"NULL"));

		return db.insert(Structure.TABLE_NAME, null, values);
	}

	public static Cursor ListCoordinates(SQLiteDatabase db, long track) {
		return db.query(Structure.TABLE_NAME, new String[] {
				Structure.LATITUDE, Structure.LONGITUDE }, Structure.TRACK
				+ " = ?", new String[] { String.valueOf(track) }, null, null,
				Structure.DATE);
	}
}
